package com.embhealth.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.embhealth.constant.MessageConstant;
import com.embhealth.entity.LogException.CheckItemDeleteFailException;
import com.embhealth.entity.PageResult;
import com.embhealth.entity.QueryPageBean;
import com.embhealth.entity.Result;
import com.embhealth.pojo.CheckGroup;
import com.embhealth.pojo.CheckItem;
import com.embhealth.service.CheckItemService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    @Reference
    private CheckItemService checkItemService;

    //新增检查项
    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        try {
            checkItemService.add(checkItem);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }
    //检查项分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkItemService.findPage(queryPageBean);
        return pageResult;
    }
    //根据id删除检查项
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            checkItemService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            if (e instanceof CheckItemDeleteFailException){
                return new Result(false,"已有检查组关联到此检查项，请先在检察组中删除此检查项");
            }
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }
    //查询检查项信息（编辑功能回显）
    @RequestMapping("/findById")
    public Result finfById(Integer id){
        try {
            CheckItem checkItem = checkItemService.findById(id);
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS,checkItem);
        }catch (Exception e){
            e.printStackTrace();
        return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //编辑检查项
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){
        try {
            checkItemService.edit(checkItem);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    /**查询检查项信息（编辑功能回显）
     * 如果不想一次性查询所有数据，可以使用分页查询方法，默认使用分页
     * @return
     */
    @RequestMapping("/findAll")
    public PageResult finfAll(Integer id,@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkItemService.findAll(id, queryPageBean);
        return pageResult;
    }


}
